package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.log4j.PropertyConfigurator;

public class ServerStartPoint 
{

    public static void main(String[] args) 
    {
    	ServerSocket server = null;
    	Socket client = null;
    	BufferedReader in = null;
    	PrintWriter out = null;
    	boolean isActive = true;
    	String input;
    	PropertyConfigurator.configure("log4j.properties");

    	try {
    		System.out.println("Try to create ServerSocket..");
			server = new ServerSocket(58112, 2);
			System.out.println("ServerSocket created!");
    		System.out.println("Try to connect Client Socket..");
    		while (isActive)
    		{
    			client = server.accept();
    			System.out.println("Connection Success");
    			ClientHandler ch = new ClientHandler(client, "user");
    			ch.run();
    		}
    		
    		in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    		out = new PrintWriter(client.getOutputStream(), true);    		
        	while ((input = in.readLine()) != null)
        	{
        		if (input.equalsIgnoreCase("exit"))
        			break;
        		out.println("S  >>>>> " + input);
        		System.out.println(input);
        	}
        	out.close();
        	in.close();
        	client.close();
        	server.close();        	
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	
    	//ConfigurationFactory.CONFIGURATION_FACTORY_PROPERTY.
    	
    	System.out.println("blabla");    	
    }
}